package com.yuke.cloud.service.omc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoDto;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.api.smc.service.SmcFeignApi;
import com.yuke.cloud.service.omc.dao.OdsAwardRecordMapper;
import com.yuke.cloud.service.omc.dto.OdsAwardRecordDto;
import com.yuke.cloud.service.omc.dto.OrderBasicDto;
import com.yuke.cloud.service.omc.dto.OrderGoodsDto;
import com.yuke.cloud.service.omc.entity.OdsAwardRecord;
import com.yuke.cloud.service.omc.entity.OdsOrder;
import com.yuke.cloud.service.omc.entity.OdsOrderGoods;
import com.yuke.cloud.service.omc.service.OdsAwardRecordService;
import com.yuke.cloud.service.omc.service.OdsOrderGoodsService;
import com.yuke.cloud.service.omc.service.OdsOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zn on 2020/10/17.
 */
@Service
@Transactional
public class OdsAwardRecordServiceImpl extends BaseService<OdsAwardRecord> implements OdsAwardRecordService {
    @Resource
    private OdsAwardRecordMapper odsAwardRecordMapper;
    @Resource
    private SmcFeignApi smcFeignApi;
    @Resource
    private PmcFeignApi pmcFeignApi;

    @Resource
    private OdsOrderService odsOrderService;
    @Resource
    private OdsOrderGoodsService odsOrderGoodsService;

    @Override
    public int saveRecord(OdsAwardRecord odsAwardRecord){
        //查询是否有未打印的订单，如果有在该订单上追加，同时改变兑换单的状态
        Example example = new Example(OdsOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderType",2);
        criteria.andLessThan("shippingStatus",2);
        criteria.andEqualTo("userId",odsAwardRecord.getUserId());
        criteria.andLessThan("orderStatus",2);
        List<OdsOrder> list = odsOrderService.selectByExample(example);
        if (list != null && list.size() > 0){
            odsAwardRecord.setAwardStatus((byte)1);
            odsAwardRecord.setOrderId(list.get(0).getOrderId());

            OdsOrderGoods odsOrderGoods = new OdsOrderGoods();
            odsOrderGoods.setOrderId(list.get(0).getOrderId());
            odsOrderGoods.setGoodsNum(odsAwardRecord.getAwardNumber());
            odsOrderGoods.setGoodsPrice(0);
            odsOrderGoods.setPayPrice(0);
            odsOrderGoods.setGoodsStatus((byte)4);
            odsOrderGoods.setSpecId(odsAwardRecord.getSpecId());
            odsOrderGoods.setWarehouseId(odsAwardRecord.getWarehouseId());
            odsOrderGoodsService.save(odsOrderGoods);
        }

        return odsAwardRecordMapper.insertSelective(odsAwardRecord);
    }

    @Override
    public List<OdsAwardRecordDto> selectAwardRecordByCondition(OdsAwardRecord odsAwardRecord){
        return odsAwardRecordMapper.selectAwardRecordByCondition(odsAwardRecord);
    }

    @Override
    public List<OdsAwardRecordDto> getExtraInfo(List<OdsAwardRecordDto> odsAwardRecordList){
        List<Long> shopIds = odsAwardRecordList.stream().map(OdsAwardRecordDto::getShopId).collect(Collectors.toList());
        List<ShopInfoDto> shopInfoDtoList = smcFeignApi.getShopInfoByShopIds(shopIds);
        if (shopInfoDtoList == null || shopInfoDtoList.size() < 1){
            return odsAwardRecordList;
        }

        List<Long> specIds = odsAwardRecordList.stream().map(OdsAwardRecordDto::getSpecId).collect(Collectors.toList());
        List<ProdSpecInfoDto> prodSpecInfoDtos  = pmcFeignApi.getSpecInfoBySpecIds(specIds);
        if (prodSpecInfoDtos == null || prodSpecInfoDtos.size() < 1){
            return odsAwardRecordList;
        }

        for (OdsAwardRecordDto odsAwardRecordDto : odsAwardRecordList){
            for (ShopInfoDto shopInfoDto : shopInfoDtoList){
                if (odsAwardRecordDto.getShopId().equals(shopInfoDto.getShopId())) {
                    odsAwardRecordDto.setShopName(shopInfoDto.getShopName());
                }
            }
            for(ProdSpecInfoDto prodSpecInfoDto : prodSpecInfoDtos){
                if (odsAwardRecordDto.getSpecId().equals(prodSpecInfoDto.getSpecId())){
                    odsAwardRecordDto.setPicUrl(prodSpecInfoDto.getBigPic());
                }
            }
        }

        return odsAwardRecordList;
    }

    /**
     * 查询是否有未打印的订单，如果有在该订单上追加，同时改变兑换单的状态
     * @param orderBasicDto
     * @return
     */
    @Override
    public List<OrderGoodsDto> getAwardRecordList(OrderBasicDto orderBasicDto){
        List<OrderGoodsDto> orderGoodsList = new ArrayList<>();
        //查询是否有未打印的订单，如果有在该订单上追加，同时改变兑换单的状态
        Example example = new Example(OdsAwardRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId",orderBasicDto.getShopId());
        criteria.andEqualTo("awardStatus",0);
        List<OdsAwardRecord> list = odsAwardRecordMapper.selectByExample(example);

        if (list == null || list.size() < 1){
            return orderGoodsList;
        }

        for (OdsAwardRecord record : list){
            //在相同仓库的情况下才绑定
            if (record.getWarehouseId().equals(orderBasicDto.getWarehouseId())){
                OrderGoodsDto orderGoods = new OrderGoodsDto();
                orderGoods.setOrderId(orderBasicDto.getOrderId());
                orderGoods.setSpecId(record.getSpecId());
                orderGoods.setGoodsNum(record.getAwardNumber());
                orderGoods.setGoodsPrice(0);
                orderGoods.setPayPrice(0);
                orderGoods.setGoodsStatus((byte)4);
                orderGoods.setWarehouseId(record.getWarehouseId());
                orderGoodsList.add(orderGoods);

                record.setAwardStatus((byte)1);
                record.setOrderId(list.get(0).getOrderId());
                odsAwardRecordMapper.updateByPrimaryKeySelective(record);
            }
        }

        return orderGoodsList;
    }

    /**
     * 解除绑定的订单，恢复到未绑定状态。订单已发货不解绑
     * @param odsOrder
     */
    @Override
    public void rebackAwardRecord(OdsOrder odsOrder){
        Example example = new Example(OdsAwardRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",odsOrder.getOrderId());
        criteria.andEqualTo("awardStatus",1);
        List<OdsAwardRecord> list = odsAwardRecordMapper.selectByExample(example);

        if (list == null || list.size() <1){
            return;
        }

        for (OdsAwardRecord record : list){
            record.setAwardStatus((byte)0);
            record.setOrderId(0L);
            odsAwardRecordMapper.updateByPrimaryKeySelective(record);
        }
    }

    /**
     * 发货时，调用，改变兑奖单状态
     * @param orderId
     */
    @Override
    public void sendAwardRecord(Long orderId){
        Example example = new Example(OdsAwardRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",orderId);
        criteria.andEqualTo("awardStatus",1);
        List<OdsAwardRecord> list = odsAwardRecordMapper.selectByExample(example);

        if (list == null || list.size() <1){
            return;
        }

        for (OdsAwardRecord record : list){
            record.setAwardStatus((byte)2);
            record.setOrderId(0L);
            odsAwardRecordMapper.updateByPrimaryKeySelective(record);
        }
    }
}
